package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectSourceDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectSource;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class GraduateProjectSourceService {
    private GraduateProjectSourceDao graduateProjectSourceDao =
            GraduateProjectSourceDao.getInstance();
    //本类的一个对象引用，保存自身对象
    private static GraduateProjectSourceService graduateProjectSourceService =
            new GraduateProjectSourceService();
    //私有的构造方法，防止其它类创建它的对象
    private GraduateProjectSourceService(){}
    //静态方法，返回本类的惟一对象
    public synchronized static GraduateProjectSourceService getInstance() {

        return graduateProjectSourceService;
    }

    public Collection<GraduateProjectSource> findAll(List conditionList,
                                                     Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<GraduateProjectSource> graduateProjectSource =
                graduateProjectSourceDao.findAll(conditionList,pagination,connection) ;
        JdbcHelper.close(connection);
        return graduateProjectSource;
    }

    public GraduateProjectSource find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        GraduateProjectSource graduateProjectSource =
                graduateProjectSourceDao.find(id,connection);
        JdbcHelper.close(connection);
        return graduateProjectSource;
    }

    public boolean update(GraduateProjectSource graduateProjectSource) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            //增加用户
            updated = graduateProjectSourceDao.update(graduateProjectSource,connection);
            //提交事务
            connection.commit();

        }catch (Exception e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated ;
    }

    public boolean add(GraduateProjectSource graduateProjectSource) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            //增加用户
            added = graduateProjectSourceDao.add(graduateProjectSource,connection);
            //提交事务
            connection.commit();

        }catch (Exception e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }

    public boolean delete(Integer id) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted =false;
        try {
            //增加用户
            deleted = graduateProjectSourceDao.delete(id,connection);
            //提交事务
            connection.commit();

        }catch (Exception e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted ;
    }
}
